//
//  NewfeatureCollectionViewController.swift
//  WEIBO
//
//  Created by 李龙 on 15/6/27.
//  Copyright © 2015年 itheima. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class NewfeatureCollectionViewController: UICollectionViewController {

    let layout = UICollectionViewFlowLayout()
    init()
    {
        super.init(collectionViewLayout: layout)
    }
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        layout.itemSize = view.bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        
        collectionView?.pagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.bounces = false //弹簧
    }
    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func viewDidLoad() {
        super.viewDidLoad()

        self.collectionView!.registerClass(newFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    let imageCount = 4
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return imageCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath)  as! newFeatureCell
//        cell.backgroundColor = UIColor.redColor()
        cell.imageIndex = indexPath.item
        // Configure the cell
    
        return cell
    }
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        let path = collectionView.indexPathsForVisibleItems().last!
        if path.item == imageCount-1
        {
            (collectionView.cellForItemAtIndexPath(path) as! newFeatureCell).showStartButton()
        }
    }
}
class newFeatureCell: UICollectionViewCell {
    var imageIndex: Int = 0 {
        didSet{
            iconView.image = UIImage(named: "new_feature_\(imageIndex+1)")
            startButton.hidden = true
        }
    }
    lazy var iconView: UIImageView =
    {
        return UIImageView()
    }()
    lazy var startButton: UIButton =
    {
        let btn = UIButton()
        btn.frame = CGRectMake(0, 0, 105, 36)
        btn.setTitle("开始体验", forState: UIControlState.Normal)
       // btn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        btn.addTarget(self, action: "startBUttonClick", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    func startBUttonClick()
    {
        NSNotificationCenter.defaultCenter().postNotificationName(HMSwitchRootVCNotification, object: true)
    }
    func showStartButton()
    {
        print("1")
        startButton.hidden = false
        startButton.userInteractionEnabled = false
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        UIView.animateWithDuration(1.0, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: UIViewAnimationOptions.CurveEaseIn, animations: { () -> Void in
            self.startButton.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                self.startButton.userInteractionEnabled = true
        }
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        iconView.frame = self.bounds
        
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(iconView)
        addSubview(startButton)
        startButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0.0))
        addConstraint(NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: -160))
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
